<!--
 * @Author: anshengtyp
 * @Date: 2023-12-05 10:53:58
<<<<<<< HEAD
 * @LastEditTime: 2024-01-11 08:31:43
=======
 * @LastEditTime: 2024-01-11 08:30:07
>>>>>>> typ-dev
 * @LastEditors: anshengtyp
 * @Description: 节点列表
-->
<template>
  <div class="app-container">
    <el-form :model="topForm" ref="topForm" size="small" :inline="true" label-width="90px">
      <el-form-item label="流程名称" prop="name">
        <el-input :disabled="true" v-model="topForm.name" placeholder="请输入流程名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="流程关键字" prop="key">
        <el-input :disabled="true" v-model="topForm.key" placeholder="请输入流程关键值" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增流程节点</el-button>
      </el-col>
      <el-button type="primary" plain icon="el-icon-refresh-right" size="mini" @click="goback">返回</el-button>
      <right-toolbar :search="false" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="nodeList">
      <el-table-column type="index" width="90" label="序号" align="center" />
      <el-table-column label="节点名称" align="center" prop="name" />
      <el-table-column label="前节点名称" align="center" prop="previousId">
        <template slot-scope="scope">
          <dict-tag :value="scope.row.previousId" :options="nodeOptions"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="承办角色" align="center" prop="undertakeRole">
        <template slot-scope="scope">
          <dict-tag :value="scope.row.undertakeRole" :options="rolesList"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="承办人" align="center" prop="undertakeUser">
        <template slot-scope="scope">
          <dict-tag :value="scope.row.undertakeUser" :options="userList"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="截止天数" align="center" prop="deadline" />
      <el-table-column label="节点关键字" align="center" prop="key" />
      <el-table-column label="排序" align="center" prop="orderNo" />
      <el-table-column label="操作" align="center" width="250">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="节点名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入节点名称" />
        </el-form-item>
        <el-form-item label="前节点名称" prop="previousId">
          <el-select filterable v-model="form.previousId" clearable placeholder="请选择前一个节点名称" style="width: 100%;">
            <el-option v-for="item in nodeOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="承办角色" prop="undertakeRole">
          <el-select filterable v-model="form.undertakeRole" clearable placeholder="请选择承办角色" style="width: 100%;">
            <el-option v-for="item in rolesList" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="承办人" prop="undertakeUser">
          <el-select filterable v-model="form.undertakeUser" clearable placeholder="请选择承办人" style="width: 100%;">
            <el-option v-for="item in userList" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="截止天数" prop="deadline">
          <el-input-number v-model="form.deadline" placeholder="请输入" :min="0" style="width: 100%;" />
        </el-form-item>
        <el-form-item label="排序" prop="orderNo">
          <el-input-number v-model="form.orderNo" placeholder="请输入" :min="0" style="width: 100%;" />
        </el-form-item>
        <el-form-item label="节点关键字" prop="key">
          <el-input v-model="form.key" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { nodelList, getNode, delNode, addNode, updateNode, getFlow } from "@/api/system/flow";
import { getAllUserList } from "@/api/system/user";
import { getAllRolesList } from "@/api/system/role";

export default {
  data() {
    return {
      // 遮罩层
      loading: true,
      // 岗位表格数据
      nodeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {},
      topForm: {},
      // 表单参数
      form: {},
      userList: [],
      // 表单校验
      rules: {
        name: [
          { required: true, message: "节点名称不能为空", trigger: "blur" }
        ],
        // undertakeRole: [
        //   { required: true, message: "承办角色不能为空", trigger: "blur" }
        // ],
        // undertakeUser: [
        //   { required: true, message: "承办人不能为空", trigger: "blur" }
        // ],
        deadline: [
          { required: true, message: "截止天数不能为空", trigger: "blur" }
        ],
        key: [
          { required: true, message: "节点关键字不能为空", trigger: "blur" }
        ],
        orderNo: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ]
      },
      rolesList: [],
      nodeOptions: []
    };
  },
  created() {
    this.getData();
    this.getFlow();
    this.getList();
  },
  methods: {
    // 获取流程详情
    getFlow() {
      getFlow(this.$route.query.id).then(response => {
        this.topForm = response.data;
      });
    },
    async getData() {
      const res = await getAllUserList();
      const res1 = await getAllRolesList();
      if (res.code === 200) {
        this.userList = res.data.map(item => {
          return {
            value: item.value,
            label: item.name,
            raw: {
              listClass: ''
            }
          }
        })
      }
      this.rolesList = res1.data.map(item => {
        return {
          value: item.roleKey,
          label: item.roleName,
          raw: {
            listClass: ''
          }
        }
      })
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      this.queryParams.wfNameId = this.$route.query.id;
      nodelList(this.queryParams).then(response => {
        this.nodeList = response.rows.sort((a, b) => a.orderNo - b.orderNo);
        this.nodeOptions = response.rows.map(item => {
          return {
            value: item.id,
            label: item.name,
            raw: {
              listClass: ''
            }
          }
        })
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = this.$options.data().form;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加节点";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getNode(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改节点";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (!this.form.undertakeRole && !this.form.undertakeUser) return this.$modal.msgError("承办角色和承办人至少需要填写一个！");
          this.form.wfNameId = this.$route.query.id;
          if (this.form.id != undefined) {
            updateNode(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNode(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除节点名称为"' + row.name + '"的数据项？').then(function () {
        return delNode(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    goback() {
      this.$router.go(-1);
    }
  }
};
</script>
